package ai.people.core.common.properties.elastic;

import ai.people.netmon.framework.enums.msg.SourceAndEsIndexMapperEnum;
import ai.people.netmon.utils.StringPool;
import ai.people.netmon.utils.StringUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ES索引映射关系
 *
 * @author yuanqinglong
 * @date 2022/5/9 13:28
 */
@Component
@ConfigurationProperties(prefix = "elastic")
public class ElasticIndex {

    /**
     * 索引名称映射
     */
    private static Map<String, String> indexMapping = new HashMap<>();

    /**
     * 索引映射
     */
    private static List<IndexInfo> index;

    /**
     * 配置未传信源名称、默认全部索引名称
     */
    private static String allIndex;


    @PostConstruct
    private void init() {
        allIndex = Optional.ofNullable(allIndex).orElse("/data-*");
        indexMapping = Optional.ofNullable(index).map(indexInfos -> index.stream().collect(Collectors.toMap(IndexInfo::getSourceName, IndexInfo::getIndexName, (s, s2) -> s2)))
                .orElseGet(() -> Arrays.stream(SourceAndEsIndexMapperEnum.values()).collect(Collectors.toMap(Enum::name, SourceAndEsIndexMapperEnum::getIndex)));
    }


    /**
     * 得到索引名称
     *
     * @param sourceName 源名称
     * @return {@link String}
     */
    public static String getIndexName(String sourceName) {
        if (StringUtils.isNotEmpty(sourceName)) {
            Set<String> sources = Arrays.stream(sourceName.split(StringPool.COMMA)).collect(Collectors.toSet());
            List<String> filterSources = sources.stream().filter(name -> indexMapping.containsKey(name)).map(source -> indexMapping.get(source)).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(filterSources)){
                return null;
            }
            return StringPool.SLASH + String.join(StringPool.COMMA, filterSources);
        }
        return StringPool.SLASH + indexMapping.getOrDefault(sourceName, allIndex);
    }


    /**
     * es模板索引名称
     *
     * @param sourceName 源名称
     * @return {@link String}
     */
    public static String getEsTemplateIndexName(String sourceName){
        if (StringUtils.isNotEmpty(sourceName)) {
            Set<String> sources = Arrays.stream(sourceName.split(StringPool.COMMA)).collect(Collectors.toSet());
            List<String> filterSources = sources.stream().filter(name -> indexMapping.containsKey(name)).map(source -> indexMapping.get(source)).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(filterSources)){
                return null;
            }
            return String.join(StringPool.COMMA, filterSources);
        }
        return indexMapping.getOrDefault(sourceName, allIndex);
    }





    /**
     * 得到平台分类
     *
     * @param platform 平台
     * @return {@link String}
     */
    public static String getPlatformClassification(String platform){
        String name = indexMapping.get(platform);
        if (StringUtils.isNotEmpty(name)){
            if (name.contains("-im-")){
                return "im";
            }
            if (name.contains("-social-")){
                return "social";
            }
            if (name.contains("-web-")){
                return "web";
            }
        }
        return "0";
    }


    public void setIndex(List<IndexInfo> index) {
        ElasticIndex.index = index;
    }

    public void setAllIndex(String allIndex) {
        ElasticIndex.allIndex = allIndex;
    }

    @Data
    static class IndexInfo {
        /**
         * 源名称
         */
        private String sourceName;
        /**
         * 索引名称
         */
        private String indexName;
    }

    /**
     * 得到所有信源名称
     *
     * @return {@link List}<{@link String}>
     */
    public List<String> getAllInformationSourceNames(){
        return index.stream().map(IndexInfo::getSourceName).collect(Collectors.toList());
    }

    public static String getAllIndex() {
        return allIndex;
    }
}
